using Microsoft.AspNetCore.Mvc;
using ERPBackend.Models;
using ERPBackend.Services;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace ERPBackend.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class SecondLevelCategoriesController : ControllerBase
    {
        private readonly ISecondLevelCategoryService _secondLevelCategoryService;

        public SecondLevelCategoriesController(ISecondLevelCategoryService secondLevelCategoryService)
        {
            _secondLevelCategoryService = secondLevelCategoryService;
        }

        // GET: api/SecondLevelCategories
        [HttpGet]
        public async Task<ActionResult<IEnumerable<SecondLevelCategory>>> GetSecondLevelCategories()
        {
            var categories = await _secondLevelCategoryService.GetAllSecondLevelCategoriesAsync();
            return Ok(categories);
        }

        // GET: api/SecondLevelCategories/5
        [HttpGet("{id}")]
        public async Task<ActionResult<SecondLevelCategory>> GetSecondLevelCategory(int id)
        {
            var secondLevelCategory = await _secondLevelCategoryService.GetSecondLevelCategoryByIdAsync(id);

            if (secondLevelCategory == null)
            {
                return NotFound();
            }

            return secondLevelCategory;
        }

        // GET: api/SecondLevelCategories/ByFirstLevelCategory/5
        [HttpGet("ByFirstLevelCategory/{firstLevelCategoryId}")]
        public async Task<ActionResult<IEnumerable<SecondLevelCategory>>> GetSecondLevelCategoriesByFirstLevelCategory(int firstLevelCategoryId)
        {
            var categories = await _secondLevelCategoryService.GetSecondLevelCategoriesByFirstLevelCategoryIdAsync(firstLevelCategoryId);
            if (categories == null)
            {
                return NotFound("Error retrieving second level categories");
            }
            return Ok(categories);
        }

        // PUT: api/SecondLevelCategories/5
        [HttpPut("{id}")]
        public async Task<IActionResult> PutSecondLevelCategory(int id, SecondLevelCategory secondLevelCategory)
        {
            if (id != secondLevelCategory.Id)
            {
                return BadRequest();
            }

            try
            {
                var updated = await _secondLevelCategoryService.UpdateSecondLevelCategoryAsync(secondLevelCategory);
                if (!updated)
                {
                    return NotFound();
                }
                return NoContent();
            }
            catch (ArgumentException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        // POST: api/SecondLevelCategories
        [HttpPost]
        public async Task<ActionResult<SecondLevelCategory>> PostSecondLevelCategory(SecondLevelCategory secondLevelCategory)
        {
            try
            {
                var createdCategory = await _secondLevelCategoryService.CreateSecondLevelCategoryAsync(secondLevelCategory);
                return CreatedAtAction("GetSecondLevelCategory", new { id = createdCategory.Id }, createdCategory);
            }
            catch (ArgumentException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        // DELETE: api/SecondLevelCategories/5
        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteSecondLevelCategory(int id)
        {
            var deleted = await _secondLevelCategoryService.DeleteSecondLevelCategoryAsync(id);
            if (!deleted)
            {
                return NotFound();
            }
            return NoContent();
        }
    }
}